package Controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"yz-go/Common"
	"yz-go/Logic"
	"yz-go/Models"
	"yz-go/Requests"
)

func list(c *gin.Context) {
	//var jobs []Models.RecruitJob

	//jobs := []map[string]interface{}{}
	var jobs []map[string]interface{}
	//w := make(map[string]interface{}) Strconv

	var totalRows int64
	var totalPages int
	var err error
	page, size := 1, 10

	// 解析页码和每页条数参数
	pageStr := c.Query("page")
	if pageStr != "" {
		res := Common.Strconv(pageStr)
		if res.Status == Common.ERROR {
			Common.ErrorResponse(c, Common.ERROR, res.Msg)
			return
		}
		page = res.Inter
	}

	sizeStr := c.Query("size")
	if sizeStr != "" {
		res := Common.Strconv(sizeStr)
		if res.Status == Common.ERROR {
			Common.ErrorResponse(c, Common.ERROR, res.Msg)
			return
		}
		size = res.Inter
	}

	query := Models.DB.Table("recruit_job as a")

	query = query.Where("a.is_deleted = ? ", Common.NOT_DELETE)

	workExp := c.Query("work_exp")
	if workExp != "" {
		query = query.Where("a.work_exp = ? ", workExp)
	}

	stDt := c.Query("st_dt")
	if stDt != "" {
		query = query.Where("a.created_at >= ? ", stDt)
	}

	enDt := c.Query("en_dt")
	if enDt != "" {
		query = query.Where("a.created_at <= ? ", enDt)
	}

	City := c.Query("city")
	if City != "" {
		query = query.Where("a.city = ? ", City)
	}

	Education := c.Query("education")
	if Education != "" {
		query = query.Where("a.education = ? ", Education)
	}

	Status := c.Query("status")
	if Status != "" {
		query = query.Where("a.status = ? ", Status)
	}

	Hot := c.Query("hot")
	if Hot != "" {
		query = query.Where("a.hot = ? ", Hot)
	}

	// 分类
	positionId := c.Query("position_category_child_id")
	if positionId != "" {
		query = query.Where("a.position_category_child_id = ? ", positionId)
	}

	// 获取企业id
	businessName := c.Query("business_name")
	if businessName != "" {
		var bids []int
		recruitBusinessQuery := Models.DB.Table("recruit_business")
		recruitBusinessQuery = recruitBusinessQuery.Where("name like ? ", "%"+businessName+"%")
		recruitBusinessQuery.Select("id").Find(&bids)
		if len(bids) > 0 {
			query = query.Where("a.business_id in ? ", bids)
		}
	}

	field := "a.id,a.business_id,c.name as business_name,a.position_custom_name," +
		"a.position_category_child_id,b.name as position_category_child_name, a.work_exp," +
		"a.education,a.salary1,a.salary2,a.salary_month,a.welfare,a.city,a.area,a.email," +
		"a.release,a.release_job,a.job_type,a.release_mobile,a.describe,a.hot,a.show,a.created_at," +
		"a.status"
	query = query.Select(field)
	query = query.Joins("left join recruit_position_category as b ON a.position_category_child_id = b.id")
	query = query.Joins("left join recruit_business as c ON a.business_id = c.id")

	// 获取总数
	err = query.Count(&totalRows).Error
	if err != nil {
		Common.ErrorResponse(c, Common.ERROR, err)
		return
	}
	if totalRows%int64(size) == 0 {
		totalPages = int(totalRows) / size
	} else {
		totalPages = int(totalRows)/size + 1
	}

	query = query.Limit(size).Offset((page - 1) * size).Order("a.created_at desc")
	err = query.Debug().Find(&jobs).Error

	if err != nil {
		Common.ErrorResponse(c, Common.ERROR, err)
		return
	}

	res := map[string]interface{}{"page": page, "size": size, "total": totalPages, "list": jobs}
	Common.SuccessResponse(c, Common.SUCCESS, res)
}

// CreateNotes 新增修改职位
func CreateNotes(c *gin.Context) {
	var p Requests.NoteParams

	uErr := c.ShouldBind(&p)

	if uErr != nil {
		fmt.Println("------------logic1-------------")

		id, err := c.Get("id")
		fmt.Println("------------logic2-------------")

		if err != true {
			Common.ErrorResponse(c, Common.SUCCESS, "未登录")
		}
		res := Logic.CreateNotes(p, int64(id.(int)))

		Common.SuccessResponse(c, Common.SUCCESS, res)
	}

}

func GetNotes(c *gin.Context) {

	mobile := c.Query("mobile")
	fmt.Println(mobile)

	res := Logic.GetNoteByMobile(mobile)

	Common.SuccessResponse(c, Common.SUCCESS, res)
}

func GetJobListByModel(c *gin.Context) {
	fmt.Println("------------note-------------")
}
